package com.ruoyi.pm.service.impl;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pubfun.IdWorker;
import com.ruoyi.pm.domain.ExcPort;
import com.ruoyi.pm.domain.SaasSupplier;
import com.ruoyi.pm.mapper.SaasSupplierMapper;
import com.ruoyi.pm.service.IExcPortService;
import com.ruoyi.pm.service.ISaasSupplierMainService;
import com.ruoyi.pm.service.ISaasSupplierRequestService;
import com.ruoyi.pm.service.ISaasSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 供应商信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-19
 */
@Service
public class SaasSupplierServiceImpl implements ISaasSupplierService 
{
    @Autowired
    private SaasSupplierMapper saasSupplierMapper;

    @Autowired
    private ISaasSupplierMainService saasSupplierMainService;

    @Autowired
    private ISaasSupplierRequestService saasSupplierRequestService;

    @Autowired
    private IExcPortService excPortService;


    /**
     * 查询供应商信息
     * 
     * @param supplierId 供应商信息主键
     * @return 供应商信息
     */
    @Override
    public SaasSupplier selectSaasSupplierBySupplierId(Long supplierId)
    {
        return saasSupplierMapper.selectSaasSupplierBySupplierId(supplierId);
    }

    /**
     * 查询供应商信息列表
     * 
     * @param saasSupplier 供应商信息
     * @return 供应商信息
     */
    @Override
    public List<SaasSupplier> selectSaasSupplierList(SaasSupplier saasSupplier)
    {
        return saasSupplierMapper.selectSaasSupplierList(saasSupplier);
    }

    /**
     * 新增供应商信息
     * 
     * @param saasSupplier 供应商信息
     * @return 结果
     */
    @Override
    public int insertSaasSupplier(SaasSupplier saasSupplier)
    {
        saasSupplier.setSupplierId(IdWorker.getId());
        saasSupplier.setCreateBy(SecurityUtils.getUsername());
        saasSupplier.setCreateTime(DateUtils.getNowDate());
        saasSupplier.setUpdateBy(SecurityUtils.getUsername());
        saasSupplier.setUpdateTime(DateUtils.getNowDate());
        return saasSupplierMapper.insertSaasSupplier(saasSupplier);
    }

    /**
     * 修改供应商信息
     * 
     * @param saasSupplier 供应商信息
     * @return 结果
     */
    @Override
    public int updateSaasSupplier(SaasSupplier saasSupplier)
    {
        saasSupplier.setUpdateBy(SecurityUtils.getUsername());
        saasSupplier.setUpdateTime(DateUtils.getNowDate());
        return saasSupplierMapper.updateSaasSupplier(saasSupplier);
    }

    /**
     * 批量删除供应商信息
     * 
     * @param supplierIds 需要删除的供应商信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSaasSupplierBySupplierIds(Long[] supplierIds)
    {
        ExcPort excPort = new ExcPort();
        for (Long supplierId : supplierIds) {
            excPort.setSupplierId(supplierId);
            List<ExcPort> excPorts = excPortService.selectExcPortList(excPort);
            if(excPorts.size()>0){
                throw new BusinessException("需要删除的保司有对应的接口信息，不允许删除。");
            }

        }

        saasSupplierMainService.deleteSaasSupplierMainBySupplierIds(supplierIds);
        saasSupplierRequestService.deleteSaasSupplierRequestBySupplierIds(supplierIds);
        return saasSupplierMapper.deleteSaasSupplierBySupplierIds(supplierIds);
    }

    /**
     * 删除供应商信息信息
     * 
     * @param supplierId 供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteSaasSupplierBySupplierId(Long supplierId)
    {
        return saasSupplierMapper.deleteSaasSupplierBySupplierId(supplierId);
    }
}
